<?php
namespace app\console\controller;

use app\console\model\Wechat as WechatModel;
use think\Validate;
use think\Request;

class Wechat extends Base
{

    public function index(Request $request)
    {
        $map = [];
        if (!empty($this->request->param('status'))) {
            $map['status'] = $this->request->param('status');
        } 
        $list = WechatModel::order('sort asc')->where($map)->paginate(15);

        // dump(json_decode( json_encode( $list),true));exit;

        return view('index', compact('list'));
    }

    public function add()
    {

        return view('add');
    }

    public function add_post()
    {
        $validate = new Validate(
            [
                ['name','require','小程序名称不能为空'],
                ['sort','require','排序不能为空'],
            ]
        );
        $data = $this->request->param();
         // dump(json_decode( json_encode( $data),true));exit;
        if (!$validate->check($data)) {
            $res['title'] = $validate->getError();
            $res['type'] = 'error';
            return success($res);
        }

        $file = request()->file('pic');
        if ($file) {
            $info = $file->validate(['size'=>15678000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pic' );
            if($info){
                $pathinfo = pathinfo($info->getSaveName());
                $pic = $info->getSaveName();
            } else{
                $res['title'] = $file->getError();
                $res['type'] = 'error';
                return success($res);
            }
        } else{

            $res['title'] = '请上传小程序吗';
            $res['type'] = 'error';
            return success($res);
        }

        WechatModel::create([
            'pic' => $pic,
            'name' => $data['name'],
            'status' => $data['status'],
            'sort' => $data['sort']
        ]);

        $res['url'] = url('console/wechat/index');
        $res['title'] = '添加成功';
        $res['type'] = 'success';

        return success($res);

    }

    public function edit($id)
    {
        $detail = WechatModel::find($id);

        return view('edit', compact('detail'));
    }

    // 更新轮播图
    public function edit_post()
    {
        $validate = new Validate(
            [
                ['name','require','小程序名称不能为空'],
                ['sort','require','排序不能为空'],
            ]
        );
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $res['title'] = $validate->getError();
            $res['type'] = 'error';
            return success($res);
        }

        $user = WechatModel::get($data['id']);
        $file = request()->file('pic');
        if ($file) {
            $info = $file->validate(['size'=>15678000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pic' );
            if($info){
                if($user['pic']){
                    @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pic' . DS . $user['pic']);
                }
                $pathinfo = pathinfo($info->getSaveName());
                $pic = $info->getSaveName();
            } else{
                $res['title'] = $file->getError();
                $res['type'] = 'error';
                return success($res);
            }
        } else{

             $pic  = $user['pic'];
        }

        $user->pic     = $pic;
        $user->name   = $data['name'];
        $user->sort   = $data['sort'];
        $user->status   = $data['status'];
        $user->save();

        $res['url'] = url('console/wechat/index');
        $res['title'] = '更新成功';
        $res['type'] = 'success';

        return success($res);
    }

    public function delete($id)
    {
        $info = WechatModel::get($id);
        if($info['pic']){
            @unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pic' . DS . $info['pic']);
        }

        WechatModel::destroy($id);
        $res['url'] = url('console/wechat/index');
        $res['title'] = '删除成功';
        $res['type'] = 'success';

        return success($res);

    }
}
